<template>
  <view class="recommendations">
    <view 
      v-for="recommendation in safeRecommendations" 
      :key="recommendation.id" 
      class="recommendation-item"
      hover-class="item-hover"
      @click="onSelect(recommendation)"
    >
      <view class="recommendation-icon">
        <!-- 使用条件渲染，根据图标类型显示不同的图片 -->
        <uni-icons v-if="iconExists(recommendation.icon)" :type="recommendation.icon" size="24" color="#00ffff"></uni-icons>
        <image 
          v-else 
          :src="getIconSrc(recommendation.icon)" 
          class="custom-icon-image" 
          mode="aspectFit"
        ></image>
      </view>
      <view class="recommendation-content">
        <text class="recommendation-title">{{ recommendation.title }}</text>
        <text class="recommendation-description">{{ recommendation.description }}</text>
      </view>
      <view class="item-arrow">
        <uni-icons type="right" size="16" color="#ffffff"></uni-icons>
      </view>
    </view>
  </view>
</template>

<script setup>
import { computed } from 'vue';

// 定义props
const props = defineProps({
  recommendations: {
    type: Array,
    default: () => []
  }
});

// 确保recommendations数组中的每个项目都具有必要的属性
const safeRecommendations = computed(() => {
  return props.recommendations.map(rec => ({
    id: rec.id || Math.random().toString(36).substr(2, 9),
    title: rec.title || '未命名推荐',
    description: rec.description || '暂无描述',
    icon: rec.icon || 'info'
  }));
});

// 定义事件
const emit = defineEmits(['select']);

// 选择推荐方法
const onSelect = (recommendation) => {
  emit('select', recommendation);
};

// 获取自定义图标的路径
const getIconSrc = (iconName) => {
  // 图标映射表
  const iconMap = {
    'wifi': '/static/icons/wifi_blue.png',
	'refresh' : '/static/icons/refresh.png'
    // 可以添加更多图标映射
  };
  
  return iconMap[iconName] || '/static/icons/default-icon.png';
};

// 检查图标是否存在于uni-icons中
const iconExists = (iconName) => {
  // uni-icons中存在的图标
  const existingIcons = ['help', 'info', 'checkmark-circle', 'calendar'];
  return existingIcons.includes(iconName);
};
</script>

<style>
.recommendations {
  width: 100%;
}

.recommendation-item {
  display: flex;
  align-items: center;
  padding: 15px;
  background: linear-gradient(90deg, rgba(0, 255, 255, 0.1), transparent);
  border-left: 3px solid #00ffff;
  margin-bottom: 10px;
  border-radius: 0 8px 8px 0;
  width: 100%;
  box-sizing: border-box;
  position: relative; /* 添加相对定位 */
}

.item-hover {
  background: linear-gradient(90deg, rgba(0, 255, 255, 0.2), rgba(0, 255, 255, 0.05));
  transform: translateX(5px);
}

.recommendation-icon {
  margin-right: 15px;
  min-width: 40px; /* 使用最小宽度确保图标不被压缩 */
  height: 40px;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid rgba(0, 255, 255, 0.5);
  flex-shrink: 0; /* 防止图标被压缩 */
}

.recommendation-content {
  flex: 1;
  min-width: 0; /* 确保flex子项可以收缩到比内容更小 */
  padding-right: 25px; /* 为箭头留出空间 */
}

.recommendation-title {
  font-size: 16px;
  color: #ffffff;
  margin-bottom: 5px;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.recommendation-description {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  display: block;
  /* 最多显示2行，超出部分显示省略号 */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-all; /* 允许在任何字符间断行 */
}

.item-arrow {
  position: absolute;
  right: 15px;
  top: 50%;
  transform: translateY(-50%);
  flex-shrink: 0; /* 防止图标被压缩 */
}

.custom-icon-image {
  width: 24px;
  height: 24px;
}
</style>